package com.wanli.storemobile.homepage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.wanli.storemobile.R;
import com.wanli.storemobile.bean.ProductListBean;
import com.wanli.storemobile.utils.Dp2Px;
import com.wanli.storemobile.utils.GlideUtils;
import com.wanli.storemobile.utils.NumberFormatUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> {


    private List<ProductListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;

    public ProductListAdapter(List<ProductListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(ProductListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_product_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ProductListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        GlideUtils.loadImage(context, holder.ivPhoto, dataBean.getThumb());
        holder.tvName.setText(dataBean.getName());
        String money = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getPrice()) / 100);
        holder.tvMoney.setText(money);
        holder.tvClassTag.setText(dataBean.getStore_goods_category_name());
        holder.tvCount.setText(dataBean.getStock() + "");

        holder.ivEdit.setOnClickListener(v -> {
            onCallBackListener.onCallBack(2, position);
        });
        holder.ivDelete.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_photo)
        RoundedImageView ivPhoto;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_class_tag)
        TextView tvClassTag;
        @BindView(R.id.tv_count)
        TextView tvCount;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        @BindView(R.id.iv_edit)
        ImageView ivEdit;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onCallBackListener {
        void onCallBack(int type, int position);
    }
}
